#!/bin/bash
clear
echo "##########################################"
echo "#      自动安装配置 fastdfs              ##"
echo "#       Press Ctrl + C to cancel        ##"
echo "#       Any key to continue             ##"
echo "##########################################"

if [ $(whoami) != "root" ];then
	echo "请使用root权限执行命令！"
	exit 1;
fi

echo -e "=================================================================="
echo -e "\033[32m  初始化环境  \033[0m"
echo -e "=================================================================="

YUM_PACKAGE='wget git unzip zip gcc gcc-c++ make cmake automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel libevent vim'

# 定义环境及目录变量
software_dir="/usr/local/"

sys_version=`cat /etc/redhat-release |awk '{print $4}'|cut -d. -f1`
IP=`ip addr|grep "inet "|grep -v 127.0.0.1|awk '{print $2}'|cut -d/ -f1`
jvm_conf="/usr/local/elasticsearch/config/jvm.options"
sys_mem=`free -m|grep Mem:|awk '{print $2}'|awk '{sum+=$1} END {print sum/1024}'|cut -d. -f1`

# 下载路径
download_file_path="/usr/local"
download_file_url="https://gitee.com/sopcce/shell/raw/master/2.fastdfs/zip/"
download_file_libfastcommon='libfastcommon-1.0.7.tar.gz'
download_file_nginx='nginx-1.16.1.tar.gz'
download_file_FastDFS='FastDFS_v5.05.tar.gz'
download_file_nginxFS='fastdfs-nginx-module_v1.16.tar.gz'


download_file_libfastcommon_fileName='libfastcommon-1.0.7'
download_file_nginx_fileName='nginx'
download_file_FastDFS_fileName='FastDFS'
download_file_nginxFS_fileName='fastdfs-nginx-module'
fastdfs_path="/etc/fdfs/"



test -d ${fastdfs_path} &&\
eval "echo fastdfs 已经安装!;exit 1"

is64bit=$(getconf LONG_BIT)
if [ "${is64bit}" != '64' ];then
	Red_Error "抱歉, 当前版本不支持32位系统, 请使用64位系统!";
	exit 1
fi

Centos6Check=$(cat /etc/redhat-release | grep ' 6.' | grep -iE 'centos|Red Hat')
if [ "${Centos6Check}" ];then
	Red_Error "Centos6不支持安装，请更换Centos7/8安装"
	exit 1
fi

test -f /etc/redhat-release ||\
eval "echo 不支持此系统!;exit 1"

test -f /usr/bin/yum ||\
eval "echo 未安装yum!;exit 1"

echo "yum 安装 ${YUM_PACKAGE}"
yum --skip-broken --nogpgcheck install -y ${YUM_PACKAGE} >/dev/null 2>&1 ||\
eval "echo yum安装${YUM_PACKAGE}失败;exit 1"

is64bit=$(getconf LONG_BIT)
if [ "${is64bit}" != '64' ];then
	Red_Error "抱歉, 当前版本不支持32位系统, 请使用64位系统!";
	exit 1
fi

echo -e "=================================================================="
echo -e "\033[31m  下载文件  \033[0m"
echo -e "=================================================================="

cd ${download_file_path}
wget -P ${download_file_path} -O ${download_file_libfastcommon} ${download_file_url}${download_file_libfastcommon}
wget -P ${download_file_path} -O ${download_file_nginx} ${download_file_url}${download_file_nginx}
wget -P ${download_file_path} -O ${download_file_FastDFS} ${download_file_url}${download_file_FastDFS}
wget -P ${download_file_path} -O ${download_file_nginxFS} ${download_file_url}${download_file_nginxFS}
cd ${download_file_path}
ll

echo -e "=================================================================="
echo -e "\033[32m  创建文件  \033[0m"
echo -e "=================================================================="

# mkdir file
mkdir -p /home/fastdfs/
mkdir -p /home/fastdfs_storage
mkdir -p /home/fastdfs_client
mkdir -p /var/temp/nginx/client
mkdir -p /usr/local/nginx/logs
mkdir -p /usr/local/src
mkdir -p ${fastdfs_path}



echo -e "=================================================================="
echo -e "\033[32m  安转libfastcommon  \033[0m"
echo -e "=================================================================="

# 安转 libfastcommon
cd ${download_file_path}
ll
tar -zxvf ${download_file_libfastcommon} -C ${download_file_path}
cd ${download_file_libfastcommon_fileName}
./make.sh
./make.sh install
cd /usr/lib64
cp libfastcommon.so /usr/lib
cd ${download_file_path}
ll
echo -e "=================================================================="
echo -e "\033[32m  安转 fastdfs  \033[0m"
echo -e "=================================================================="

##安装 fastdfs
cd ${download_file_path}
tar -zxvf ${download_file_FastDFS} -C ${fastdfs_path}
cd ${fastdfs_path}
./make.sh
./make.sh install
cd conf
cp * ${fastdfs_path}
cd ${fastdfs_path}
ll

cd ${download_file_path}
ll /etc/init.d/ | grep fdfs


GetSysInfo


echo 'fastdfs安装完毕!'


GetSysInfo(){
	if [ -s "/etc/redhat-release" ];then
		SYS_VERSION=$(cat /etc/redhat-release)
	elif [ -s "/etc/issue" ]; then
		SYS_VERSION=$(cat /etc/issue)
	fi
	SYS_INFO=$(uname -a)
	SYS_BIT=$(getconf LONG_BIT)
	MEM_TOTAL=$(free -m|grep Mem|awk '{print $2}')
	CPU_INFO=$(getconf _NPROCESSORS_ONLN)

	echo -e ${SYS_VERSION}
	echo -e Bit:${SYS_BIT} Mem:${MEM_TOTAL}M Core:${CPU_INFO}
	echo -e ${SYS_INFO}
	echo -e "请截图以上报错信息发帖至论坛www.bt.cn/bbs求助"
}


# 主函数统一调用
main() {


  echo "url:http://$IP:5601"
}
# 执行主函数
main
